package com.tjtt.tdm.system.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import com.tjtt.common.utils.StringUtil;
import com.tjtt.tdm.base.BaseAction;
import com.tjtt.tdm.base.PageResult;
import com.tjtt.tdm.system.service.RightGiveService;

public class RightGiveAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	private RightGiveService rightGiveService;

	public RightGiveService getRightGiveService() {
		return rightGiveService;
	}

	public void setRightGiveService(RightGiveService rightGiveService) {
		this.rightGiveService = rightGiveService;
	}
	
	private String rightgiveid;
	private String sourcepersonid;
	private String sourcepersonname;
	private String targetpersonid;
	private String targetpersonname;
	private String begindate;
	private String enddate;
	
	public String getRightgiveid() {
		return rightgiveid;
	}

	public void setRightgiveid(String rightgiveid) {
		this.rightgiveid = rightgiveid;
	}

	public String getSourcepersonid() {
		return sourcepersonid;
	}

	public void setSourcepersonid(String sourcepersonid) {
		this.sourcepersonid = sourcepersonid;
	}

	public String getSourcepersonname() {
		return sourcepersonname;
	}

	public void setSourcepersonname(String sourcepersonname) {
		this.sourcepersonname = sourcepersonname;
	}

	public String getTargetpersonid() {
		return targetpersonid;
	}

	public void setTargetpersonid(String targetpersonid) {
		this.targetpersonid = targetpersonid;
	}

	public String getTargetpersonname() {
		return targetpersonname;
	}

	public void setTargetpersonname(String targetpersonname) {
		this.targetpersonname = targetpersonname;
	}

	public String getBegindate() {
		return begindate;
	}

	public void setBegindate(String begindate) {
		this.begindate = begindate;
	}

	public String getEnddate() {
		return enddate;
	}

	public void setEnddate(String enddate) {
		this.enddate = enddate;
	}

	public void getRightGiveList() throws Exception{
		try {
			Map<String, String> filterMap = getPagerMap();
		    //取出页面中的查询条件
			String sourcepersonname = getRequest().getParameter("sourcepersonname");
			//解决乱码问题
			if(sourcepersonname != null && !"".equals(sourcepersonname)){
				sourcepersonname = StringUtil.encodingUrl(sourcepersonname);
			}
			//将查询条件保存到Map里
			filterMap.put("sourcepersonname", sourcepersonname);
			String[] viewArray = { "ID", 
					"SOURCEPERSONNAME","TARGETPERSONNAME", "BEGINDATE","ENDDATE"};
			// 查询所有的数据
			PageResult result = rightGiveService.queryRightGiveList(filterMap);
			String returnStr = getColumnJson(result, viewArray);
			setContentType();
			PrintWriter out = getResponse().getWriter();
			System.out.println(returnStr);
			out.print(returnStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void saveRightGive() throws IOException {
		PrintWriter out = getResponse().getWriter();
		try {
			String sourcepersonid = getRequest().getParameter("sourcepersonid");
			String sourcepersonname = getRequest().getParameter("sourcepersonname");
			String targetpersonname = getRequest().getParameter("targetpersonname");
			String targetpersonid = getRequest().getParameter("targetpersonid");
			String begindate = getRequest().getParameter("begindate");
			String enddate = getRequest().getParameter("enddate");
			String rightgiveid = getRequest().getParameter("rightgiveid");
			if (rightgiveid==null){
				String strSql = "insert into SYS_RIGHT_GIVE(id,SOURCEPERSONID,SOURCEPERSONNAME,TARGETPERSONID," +
				"TARGETPERSONNAME,BEGINDATE,ENDDATE) values (seq_sys_right_give_id.nextval,'"+sourcepersonid+"','"+sourcepersonname+"'," +
						"'"+targetpersonid+"','"+targetpersonname+"','"+begindate+"','"+enddate+"')";
				rightGiveService.exeSql(strSql);
			}
			else{
				String strSql = "update SYS_RIGHT_GIVE set SOURCEPERSONID='"+sourcepersonid+"',SOURCEPERSONNAME='"+sourcepersonname+"'," +
						"TARGETPERSONID='"+targetpersonid+"',TARGETPERSONNAME='"+targetpersonname+"'," +
					    "BEGINDATE='"+begindate+"',ENDDATE='"+enddate+"' " +
						" where id='"+rightgiveid+"'";
				rightGiveService.exeSql(strSql);
			}
			out.print("{\"result\":\"success\"}");
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
			out.print("{\"result\":\"error\"}");
			out.flush();
			out.close();
		}
	}
	
	public String initEditRightGive(){
		try {
			String rightgiveid = getRequest().getParameter("rightgiveid");
			String strSql = "select t.id,t.sourcepersonid,t.sourcepersonname,t.targetpersonid,t.targetpersonname," +
					"t.begindate,t.enddate from sys_right_give t where t.id="+rightgiveid;
			List lists = rightGiveService.getSql(strSql);
			if (lists.size()>0){
				 Object[] objs = (Object[])lists.get(0);
				 rightgiveid = objs[0].toString();
				 if (objs[1]!=null){
					 sourcepersonid = objs[1].toString();
					 sourcepersonname = objs[2].toString();
				 }
				 if (objs[3]!=null){
					 targetpersonid = objs[3].toString();
					 targetpersonname = objs[4].toString();
				 }
				 if  (objs[5]!=null){
					 begindate = objs[5].toString();
				 }
				 if  (objs[5]!=null){
					 enddate = objs[6].toString();
				 }
			}
			return "toedit";
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}
	
	public void deleteRightGive() throws IOException{
		PrintWriter out = getResponse().getWriter();
		try {
			String rightgiveids = getRequest().getParameter("rightgiveids");
			String strSql = "delete from SYS_RIGHT_GIVE  where id in ("+rightgiveids+")";
	rightGiveService.exeSql(strSql);
			out.print("{\"result\":\"success\"}");
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
			out.print("{\"result\":\"error\"}");
			out.flush();
			out.close();
		}
	}
}
